package com.ruoyi.xiaoshou.service.impl;

import com.ruoyi.system.api.RemoteStockInDetailService;
import com.ruoyi.system.api.RemoteStockInService;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.system.api.domain.store.StockInDetail;
import com.ruoyi.xiaoshou.domain.SaleBackDetail;
import com.ruoyi.xiaoshou.mapper.SaleBackDetailMapper;
import com.ruoyi.xiaoshou.service.ISaleBackDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 销售退货详情Service业务层处理
 *
 * @author lmj
 * @date 2024-10-29
 */
@Service
@Transactional
@Slf4j
public class SaleBackDetailServiceImpl implements ISaleBackDetailService {
    @Autowired
    private SaleBackDetailMapper saleBackDetailMapper;
    @Autowired
    private RemoteStockInService remoteStockInService;
    @Autowired
    private RemoteStockInDetailService remoteStockInDetailService;


    /**
     * 查询销售退货详情
     *
     * @param id 销售退货详情主键
     * @return 销售退货详情
     */
    @Override
    public SaleBackDetail selectSaleBackDetailById(Integer id) {
        return saleBackDetailMapper.selectSaleBackDetailById(id);
    }

    /**
     * 查询销售退货详情列表
     *
     * @param saleBackDetail 销售退货详情
     * @return 销售退货详情
     */
    @Override
    public List<SaleBackDetail> selectSaleBackDetailList(SaleBackDetail saleBackDetail) {
        return saleBackDetailMapper.selectSaleBackDetailList(saleBackDetail);
    }

    @Override
    public List<SaleBackDetail> useSaleBackDetailList(SaleBackDetail saleBackDetail) {
        log.info("SaleBackDetailServiceImpl useSaleBackDetailList params:{}", saleBackDetail);
        //先查询退货详情，获取详情列表，再根据编号查询入库单，由入库单查询对应的详情
        List<SaleBackDetail> saleBackDetails = saleBackDetailMapper.selectSaleBackDetailList(saleBackDetail);
        StockIn stockIn = new StockIn();
        stockIn.setOrderCode(saleBackDetail.getSaleBackCode());
        List<StockIn> stockIns = remoteStockInService.getStockList(stockIn);
        //如果申请单有对应绑定的退货订单就继续
        if (stockIns.size() == 0 || stockIns == null) {
            return saleBackDetails;
        }
        // 获取 stockInId 数组
        // 将 List 转换为数组
        Integer[] idArray = stockIns.stream().map(StockIn::getId).toArray(Integer[]::new);
        List<StockInDetail> stockInDetails = remoteStockInDetailService.getStockInDetail(idArray);
        //如果有详情就继续
        if (stockInDetails.isEmpty()) {
            return saleBackDetails;
        }
        //最终结果
        List<SaleBackDetail> saleBackDetails1 = new ArrayList<>();
        for (SaleBackDetail saleBackDetail1 : saleBackDetails) {
            int proNum = saleBackDetail1.getProNum();
            for (StockInDetail stockInDetail : stockInDetails) {
                if (saleBackDetail1.getProCode().equals(stockInDetail.getProCode())) {
                    proNum -= stockInDetail.getTotalNum();
                }
            }
            if (proNum > 0) {
                saleBackDetail1.setProNum(proNum);
                saleBackDetails1.add(saleBackDetail1);
            }
        }
        return saleBackDetails1;
    }

    /**
     * 新增销售退货详情
     *
     * @param saleBackDetail 销售退货详情
     * @return 结果
     */
    @Override
    public int insertSaleBackDetail(SaleBackDetail saleBackDetail) {
        return saleBackDetailMapper.insertSaleBackDetail(saleBackDetail);
    }

    /**
     * 修改销售退货详情
     *
     * @param saleBackDetail 销售退货详情
     * @return 结果
     */
    @Override
    public int updateSaleBackDetail(SaleBackDetail saleBackDetail) {
        return saleBackDetailMapper.updateSaleBackDetail(saleBackDetail);
    }

    /**
     * 批量删除销售退货详情
     *
     * @param ids 需要删除的销售退货详情主键
     * @return 结果
     */
    @Override
    public int deleteSaleBackDetailByIds(Integer[] ids) {
        return saleBackDetailMapper.deleteSaleBackDetailByIds(ids);
    }

    /**
     * 删除销售退货详情信息
     *
     * @param id 销售退货详情主键
     * @return 结果
     */
    @Override
    public int deleteSaleBackDetailById(Integer id) {
        return saleBackDetailMapper.deleteSaleBackDetailById(id);
    }
}
